package com.zhida.smartcontrol.dialog.nbdialog;

import android.os.Bundle;
import android.os.Parcelable;
import android.view.View;
import android.widget.FrameLayout;

import com.jay.tools.ui.ButtonStyle;
import com.jay.tools.utils.ResUtil;
import com.tan.linkpicker.pickerview.builder.OptionsPickerBuilder;
import com.tan.linkpicker.pickerview.builder.TimePickerBuilder;
import com.tan.linkpicker.pickerview.listener.CustomListener;
import com.tan.linkpicker.pickerview.listener.OnOptionsSelectChangeListener;
import com.tan.linkpicker.pickerview.listener.OnOptionsSelectListener;
import com.tan.linkpicker.pickerview.listener.OnTimeSelectChangeListener;
import com.tan.linkpicker.pickerview.view.OptionsPickerView;
import com.tan.linkpicker.pickerview.view.TimePickerView;
import com.zhida.smartcontrol.R;
import com.zhida.smartcontrol.common.bean.LampSwitchBean;
import com.zhida.smartcontrol.mvp.MVPBaseDialogFragment;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.BindViews;
import butterknife.OnClick;

/**
 * author: tanjianpei
 * date: 2019/12/12 012 15:38
 * description: 单双灯NB控时
 */
public class NBDialogFragment extends MVPBaseDialogFragment<NBDialogContract.View, NBDialogPresenter> implements NBDialogContract.View {
    @BindView(R.id.btn_all)
    ButtonStyle mBtnAll;
    @BindViews({R.id.btn_0, R.id.btn_1, R.id.btn_2, R.id.btn_3, R.id.btn_4, R.id.btn_5, R.id.btn_6})
    List<ButtonStyle> mBtnList;
    @BindViews({R.id.fl_time_1, R.id.fl_time_1_2,
            R.id.fl_time_2, R.id.fl_time_2_2,
            R.id.fl_time_3, R.id.fl_time_3_2,
            R.id.fl_time_4, R.id.fl_time_4_2})
    List<FrameLayout> mFlList;
    @BindViews({R.id.fl_time_1_3, R.id.fl_time_2_3, R.id.fl_time_3_3, R.id.fl_time_4_3})
    List<FrameLayout> mFlLightList;

    private int mSwitchType;
    private List<LampSwitchBean> mLampList;

    private String[] mTimeArr = new String[]{"", "", "", "", "", "", "", ""};
    private String[] mBrightArr = new String[]{"0", "0", "0", "0"};

    public static NBDialogFragment newInstance(int switchType, List<LampSwitchBean> data) {
        Bundle args = new Bundle();
        args.putInt("type", switchType);
        args.putParcelableArrayList("list", (ArrayList<? extends Parcelable>) data);
        NBDialogFragment fragment = new NBDialogFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected Object getLayout() {
        return R.layout.dialog_nb;
    }

    @Override
    protected void initDatas(Bundle savedInstanceState) {
        Bundle bundle = getArguments();
        if (null == bundle) {
            return;
        }
        mSwitchType = bundle.getInt("type");
        mLampList = bundle.getParcelableArrayList("list");

        for (int i = 0; i < mBtnList.size(); i++) {
            ButtonStyle button = mBtnList.get(i);
            button.setId(i);
        }

        for (int i = 0; i < mFlList.size(); i++) {
            FrameLayout frameLayout = mFlList.get(i);
            initCustomTimePicker(frameLayout, i);
        }
        getCardData();
        for (int i = 0; i < mFlLightList.size(); i++) {
            FrameLayout frameLayout = mFlLightList.get(i);
            initCustomOptionPicker(frameLayout, i);
        }
    }

    @OnClick({R.id.btn_cancel, R.id.btn_confirm, R.id.btn_all})
    public void click(View v) {
        switch (v.getId()) {
            case R.id.btn_all:
                mBtnAll.setChecked(!mBtnAll.isChecked());
                for (ButtonStyle button : mBtnList) {
                    button.setChecked(mBtnAll.isChecked());
                }
                break;
            case R.id.btn_cancel:
                dismiss();
                break;
            case R.id.btn_confirm:
                mPresenter.requestNbTime(mSwitchType, mLampList, mTimeArr, mBtnList, mBrightArr);
                break;
        }
    }

    @OnClick({R.id.btn_0, R.id.btn_1, R.id.btn_2, R.id.btn_3, R.id.btn_4, R.id.btn_5, R.id.btn_6})
    public void clickWay(View v) {
        int id = v.getId();
        mBtnAll.setChecked(false);
        mBtnList.get(id).setChecked(!mBtnList.get(id).isChecked());
    }

    private void initCustomTimePicker(FrameLayout frameLayout, final int id) {
        Calendar selectedDate = Calendar.getInstance();
        selectedDate.set(Calendar.HOUR_OF_DAY, 0);
        selectedDate.set(Calendar.MINUTE, 0);
        selectedDate.set(Calendar.SECOND, 0);

        Calendar startDate = Calendar.getInstance();
        startDate.set(2013, 0, 23);

        //时间选择器
        TimePickerView pvTime = new TimePickerBuilder(mActivity, null)
                .setLayoutRes(R.layout.layout_time_picker, new CustomListener() {
                    @Override
                    public void customLayout(View v) {

                    }
                })
                .setTimeSelectChangeListener(new OnTimeSelectChangeListener() {
                    @Override
                    public void onTimeSelectChanged(Date date) {
                        mTimeArr[id] = getTime(date);
                    }
                })
                .setItemsVisible(3)
                .setType(new boolean[]{false, false, false, true, true, false})
                .setLabel("", "", "", ":", "", "")
                .setContentTextSize(20)
                .setTextColorCenter(ResUtil.getColor(mActivity, R.color.c_3862FF))
                .setDate(selectedDate)
                .setRangDate(startDate, selectedDate)
                .setDividerColor(0x00FFFFFF)
                .setDecorView(frameLayout)
                .build();

        pvTime.setKeyBackCancelable(false);//系统返回键监听屏蔽掉
        pvTime.show();
    }

    private String getTime(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("HH:mm");
        return format.format(date);
    }

    private List<CardBean> cardItem = new ArrayList<>();

    private void getCardData() {
        cardItem.add(new CardBean(0, "0", "0")); // 0
        cardItem.add(new CardBean(1, "25", "4")); // 5
        cardItem.add(new CardBean(2, "50", "3")); //3
        cardItem.add(new CardBean(3, "75", "2")); //2
        cardItem.add(new CardBean(4, "100", "1"));//1
    }

    /**
     * 条件选择器初始化，自定义布局
     */
    private void initCustomOptionPicker(FrameLayout frameLayout, final int id) {
        /**
         * 自定义布局中，id为 optionspicker 或者 timepicker 的布局以及其子控件必须要有，否则会报空指针。
         * 具体可参考demo 里面的两个自定义layout布局。
         */
        OptionsPickerView pvCustomOptions = new OptionsPickerBuilder(mActivity, new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int option2, int options3, View v) {

            }
        })
                .setLayoutRes(R.layout.layout_option_picker, new CustomListener() {
                    @Override
                    public void customLayout(View v) {

                    }
                })
                .setOptionsSelectChangeListener(new OnOptionsSelectChangeListener() {
                    @Override
                    public void onOptionsSelectChanged(int options1, int options2, int options3) {
                        String tx = cardItem.get(options1).getType();
                        mBrightArr[id] = tx;
                    }
                })
                .isDialog(false)
                .setLabels("%", "%", "%")
                .setContentTextSize(20)
                .setItemVisible(3)
                .setDividerColor(0x00FFFFFF)
                .setTextColorCenter(ResUtil.getColor(mActivity, R.color.c_FF9A2E))
                .setDecorView(frameLayout)
                .setOutSideCancelable(false)
                .build();

        pvCustomOptions.setPicker(cardItem);//添加数据
        pvCustomOptions.show();

    }

    @Override
    public void onSuccess() {
        dismiss();
    }
}
